package com.cljh.service.impl;

import com.cljh.bean.Img;
import com.cljh.controller.page.PageBean;
import com.cljh.bean.Product;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.mapper.ImgMapper;
import com.cljh.mapper.ProductMapper;
import com.cljh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ImgMapper imgMapper;
    @Override
    public PageBean<ProductPageBean> selectAllByCondition(Integer currentPage, Integer pageSize, ProductPageBean product) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        String productName = product.getProductName();
        if (productName != null && productName.length() > 0) {
            product.setProductName("%" + productName + "%");
        }
        String productDesign = product.getProductDesign();
        if (productDesign != null && productDesign.length() > 0) {
            product.setProductDesign("%" + productDesign + "%");
        }
        //分页查询
        List<Product> products = productMapper.selectAllByCondition(begin, pageSize, product);
        List<ProductPageBean> pageBeans = new ArrayList<>();
        //遍历product集合放到封装类
        for (Product p : products) {
            ProductPageBean productPageBean = new ProductPageBean();
            productPageBean.setId(p.getId());
            productPageBean.setShowId(p.getShowId());
            productPageBean.setProductName(p.getDistinct().getProductName());
            productPageBean.setProductSum(p.getProductSum());
            productPageBean.setPrice(p.getDistinct().getPrice());
            productPageBean.setCost(p.getCost());
            productPageBean.setTypes(p.getDistinct().getTypes());
            productPageBean.setProductDesign(p.getProductDesign());
            productPageBean.setProductColor(p.getProductColor());
            productPageBean.setProductDetail(p.getProductDetail());
            productPageBean.setProductMaterials(p.getProductMaterials());
            productPageBean.setProductSize(p.getProductSize());
            productPageBean.setSeason(p.getSeason());
            productPageBean.setCreateTime(p.getCreateTime());
            productPageBean.setImg(p.getImgs().getImg());
            productPageBean.setImg1(p.getImgs().getImg1());
            productPageBean.setImg2(p.getImgs().getImg2());
            pageBeans.add(productPageBean);
        }
        int i = productMapper.selectByCount(product);
        PageBean<ProductPageBean> pageBean = new PageBean<>();
        pageBean.setRows(pageBeans);
        pageBean.setTotalCount(i);
        return pageBean;
    }

    /**
     * 单数据添加
     * @param productBean
     * @return
     */
    @Override
    public boolean save(ProductPageBean productBean) {
        
        Product product = new Product();
        product.setShowId(productBean.getShowId());
//        product.setProductName(productBean.getProductName());
        product.setProductSum(productBean.getProductSum());
//        product.setPrice(productBean.getPrice());
        product.setCost(productBean.getCost());
//        product.setTypes(productBean.getTypes());
        product.setProductDesign(productBean.getProductDesign());
        product.setProductColor(productBean.getProductColor());
        product.setProductDetail(productBean.getProductDetail());
        product.setProductMaterials(productBean.getProductMaterials());
        product.setProductSize(productBean.getProductSize());
        product.setSeason(productBean.getSeason());
        Img img = new Img();
        img.setImg(productBean.getImg());
        img.setImg1(productBean.getImg1());
        img.setImg2(productBean.getImg2());
        imgMapper.insert(img);
        product.setImgId(img.getId());

        return productMapper.save(product) > 0;
    }

    @Transactional
    @Override
    public boolean delete(Integer id) {
        Product product = productMapper.selectById(id);
        Integer imgId = product.getImgId();
        if (imgId!=null){
            imgMapper.delete(imgId);
        }
        return productMapper.delete(id) > 0;
    }


    /**
     * 更新
     * @param productBean
     * @return
     */
    @Override
    public boolean update(ProductPageBean productBean) {
        //分俩个表更新
//        Product product = new Product();
//        Img img = new Img();
//        product.setId(productBean.getId());
//        product.setProductName(productBean.getProductName());
//        product.setProductSum(productBean.getProductSum());
//        product.setPrice(productBean.getPrice());
//        product.setTypes(productBean.getTypes());
//        product.setProductDesign(productBean.getProductDesign());
//        product.setProductColor(productBean.getProductColor());
//        product.setProductDetail(productBean.getProductDetail());
//        product.setProductMaterials(productBean.getProductMaterials());
//        product.setProductSize(productBean.getProductSize());
//        Product product1 = productMapper.selectById(product.getId());
//        img.setId(product1.getImgId());
//        img.setImg(productBean.getImg());
//        img.setImg1(productBean.getImg1());
//        img.setImg2(productBean.getImg2());
//        return productMapper.update(product) > 0&&imgMapper.update(img)>0;

        //使用多表联合更新语句
        return productMapper.update(productBean)>0;
    }
    @Transactional
    @Override
    public boolean deleteByIds(List<Integer> ids) {
        for (Integer i : ids) {
            Product product = productMapper.selectById(i);
            Integer imgId = product.getImgId();
            if (imgId!=null){
                imgMapper.delete(imgId);
            }
        }
        return productMapper.deleteByIds(ids) > 0;
    }


    /**
     * 查询单个数据
     * @param id
     * @return
     */
    @Override
    public ProductPageBean selectById(Integer id) {
        Product p = productMapper.selectById(id);
        ProductPageBean productPageBean=null;
        if (p!=null){
            productPageBean = new ProductPageBean();
            productPageBean.setId(p.getId());
            productPageBean.setShowId(p.getShowId());
            productPageBean.setProductName(p.getDistinct().getProductName());
            productPageBean.setProductSum(p.getProductSum());
            productPageBean.setPrice(p.getDistinct().getPrice());
            productPageBean.setCost(p.getCost());
            productPageBean.setTypes(p.getDistinct().getTypes());
            productPageBean.setProductDesign(p.getProductDesign());
            productPageBean.setProductColor(p.getProductColor());
            productPageBean.setProductDetail(p.getProductDetail());
            productPageBean.setProductMaterials(p.getProductMaterials());
            productPageBean.setProductSize(p.getProductSize());
            productPageBean.setSeason(p.getSeason());
            productPageBean.setCreateTime(p.getCreateTime());
            productPageBean.setImg(p.getImgs().getImg());
            productPageBean.setImg1(p.getImgs().getImg1());
            productPageBean.setImg2(p.getImgs().getImg2());
        }
        return productPageBean;
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<ProductPageBean> ProductList() {
        List<Product> list = productMapper.ProductList();
        List<ProductPageBean> pageBeans = new ArrayList<>();
        //遍历product集合放到封装类
        for (Product p : list) {
            ProductPageBean productPageBean = new ProductPageBean();
            productPageBean.setShowId(p.getShowId());
            productPageBean.setId(p.getId());
            productPageBean.setProductName(p.getDistinct().getProductName());
            productPageBean.setProductSum(p.getProductSum());
            productPageBean.setPrice(p.getDistinct().getPrice());
            productPageBean.setCost(p.getCost());
            productPageBean.setTypes(p.getDistinct().getTypes());
            productPageBean.setProductDesign(p.getProductDesign());
            productPageBean.setProductColor(p.getProductColor());
            productPageBean.setProductDetail(p.getProductDetail());
            productPageBean.setProductMaterials(p.getProductMaterials());
            productPageBean.setProductSize(p.getProductSize());
            productPageBean.setSeason(p.getSeason());
            productPageBean.setCreateTime(p.getCreateTime());
            productPageBean.setImg(p.getImgs().getImg());
            productPageBean.setImg1(p.getImgs().getImg1());
            productPageBean.setImg2(p.getImgs().getImg2());
            pageBeans.add(productPageBean);
        }

        return pageBeans;
    }


    @Override
    public List<ProductPageBean> ProductList1() {
        List<Product> list = productMapper.ProductList();
        List<ProductPageBean> pageBeans = new ArrayList<>();
        //遍历product集合放到封装类
        for (Product p : list) {
            ProductPageBean productPageBean = new ProductPageBean();
            productPageBean.setId(p.getId());
            productPageBean.setProductName(p.getDistinct().getProductName());
            pageBeans.add(productPageBean);
        }
        return pageBeans;
    }

    /**
     * 批量增加
     * @param products
     * @return
     */
    @Transactional
    @Override
    public boolean saveAll(List<ProductPageBean> products) {
        List<Product> list = new ArrayList<>();
        //遍历集合依次赋值添加
        for (ProductPageBean p :
                products) {

            Product product = new Product();
//            product.setProductName(p.getProductName());
            product.setProductSum(p.getProductSum());
//            product.setPrice(p.getPrice());
            product.setCost(p.getCost());
            product.setShowId(p.getShowId());
//            product.setTypes(p.getTypes());
            product.setProductDesign(p.getProductDesign());
            product.setProductColor(p.getProductColor());
            product.setProductDetail(p.getProductDetail());
            product.setProductMaterials(p.getProductMaterials());
            product.setProductSize(p.getProductSize());
            product.setSeason(p.getSeason());
            Img img = new Img();
            img.setImg(p.getImg());
            img.setImg1(p.getImg1());
            img.setImg2(p.getImg2());
            imgMapper.insert(img);
            //主键返回
            product.setImgId(img.getId());
            list.add(product);
        }
        return productMapper.saveAll(list) > 0;
    }

    @Override
    public PageBean<ProductPageBean> selectByDistinct(Integer currentPage, Integer pageSize, ProductPageBean product) {
       return null;
    }

    @Override
    public List<ProductPageBean> selectByShowId(Integer id) {
        List<Product> list = productMapper.selectByShowId(id);
        List<ProductPageBean> pageBeans = new ArrayList<>();
        for (Product p : list) {
            ProductPageBean productPageBean = new ProductPageBean();
            productPageBean.setShowId(p.getShowId());
            productPageBean.setId(p.getId());
            productPageBean.setProductName(p.getDistinct().getProductName());
            productPageBean.setProductSum(p.getProductSum());
            productPageBean.setPrice(p.getDistinct().getPrice());
            productPageBean.setCost(p.getCost());
            productPageBean.setTypes(p.getDistinct().getTypes());
            productPageBean.setProductDesign(p.getProductDesign());
            productPageBean.setProductColor(p.getProductColor());
            productPageBean.setProductDetail(p.getProductDetail());
            productPageBean.setProductMaterials(p.getProductMaterials());
            productPageBean.setProductSize(p.getProductSize());
            productPageBean.setSeason(p.getSeason());
            productPageBean.setCreateTime(p.getCreateTime());
            productPageBean.setImg(p.getImgs().getImg());
            productPageBean.setImg1(p.getImgs().getImg1());
            productPageBean.setImg2(p.getImgs().getImg2());
            pageBeans.add(productPageBean);
        }

        return pageBeans;
    }

}
